package com.sundae.smarthomedemo.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sundae.smarthomedemo.Beans.DeviceSQLBean;
import com.sundae.smarthomedemo.R;
import com.sundae.smarthomedemo.Utils.Utils;

import java.util.ArrayList;

/**
 * Created by daijiyuan on 2017/6/5.
 * 邮箱 948820549@qq.com
 *
 * @Sundae
 */

public class HomeDeviceAdapter extends BaseAdapter {

    private ArrayList<DeviceSQLBean> deviceBeanArrayList;
    private DeviceSQLBean deviceBean;
    private LayoutInflater layoutInflater;
    private Context context;

    public HomeDeviceAdapter(Context context , ArrayList<DeviceSQLBean> deviceBeanArrayList) {
        this.deviceBeanArrayList = deviceBeanArrayList;
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return deviceBeanArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return deviceBeanArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if(convertView == null)
        {
            convertView = layoutInflater.inflate(R.layout.home_device_item , null);
            viewHolder = new ViewHolder();
            viewHolder.img = (ImageView) convertView.findViewById(R.id.home_device_img);
            viewHolder.name_tv = (TextView) convertView.findViewById(R.id.device_name);
            viewHolder.status_tv = (TextView) convertView.findViewById(R.id.status_textview);
            viewHolder.model_tv = (TextView) convertView.findViewById(R.id.model_textview);
            viewHolder.temp_tv = (TextView) convertView.findViewById(R.id.temp_textview);
            convertView.setTag(viewHolder);
        }else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        deviceBean = deviceBeanArrayList.get(position);
        viewHolder.img.setImageResource(Utils.getImgRes(deviceBean.getDevice_type()));
        viewHolder.name_tv.setText(deviceBean.getDevice_name());
        viewHolder.status_tv.setText(deviceBean.getRun_status());
        viewHolder.model_tv.setText(Utils.getModelString(deviceBean.getModel_type()));
        viewHolder.temp_tv.setText(deviceBean.getTemp() + "℃");

        return convertView;
    }

    public static class ViewHolder
    {
        public ImageView img;
        public TextView name_tv;
        public TextView status_tv;
        public TextView model_tv;
        public TextView temp_tv;
    }

}
